package dao;

import douban.DouBanContent;
import model.Aws_question;
import model.Utils;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.Map;

/**
 * Created by liuyaowen on 2016/8/26.
 */
public class DoubanJDBCTemplate {
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplateObject;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplateObject = new JdbcTemplate(dataSource);
    }


    public boolean check_aws_question(String title) {
        int question = jdbcTemplateObject.queryForInt("SELECT  COUNT(*)  FROM aws_question t WHERE t.`question_content` = ?", new Object[]{title});
        if (question > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    public boolean insert_aws_question(DouBanContent douBanContent) {
        int question_id = load_index_id("aws_question", "question_id");
        StringBuffer insertSql = new StringBuffer();
        insertSql.append("INSERT INTO `aws_question` SET question_id='" + question_id + "',")
                .append("question_content='" + douBanContent.getTitle() + "'")//需要填写的
                .append(",question_detail='" + douBanContent.getContent() + "'")//需要填写的
                .append(",add_time='" + System.currentTimeMillis() / 1000L + "'")//需要填写的
                .append(",update_time='" + System.currentTimeMillis() / 1000L + "'")//需要填写的
                .append(",published_uid='1',answer_count='"+douBanContent.getReviews().size()+"',answer_users='"+douBanContent.getReviews().size()+"',view_count='"+douBanContent.getReviews().size()+"',focus_count='0',comment_count='"+douBanContent.getReviews().size()+"'")//需要填写的
                .append(",action_history_id='0',category_id='" + douBanContent.getCategory_id() + "',agree_count='0',against_count='0',best_answer='0',has_attach='0'")//需要填写的
                .append(",unverified_modify_count='0',ip='2130706433',last_answer='0'")//需要填写的
                .append(",popular_value='0',popular_value_update='" + System.currentTimeMillis() / 1000L + "',`lock`='0',anonymous='0',thanks_count='0'")//需要填写的
                .append(",question_content_fulltext='" + Utils.get_question_content_fulltext(douBanContent.getTitle()) + "'")//需要填写的
                .append(",is_recommend='0',sort='0'");

        int count = jdbcTemplateObject.update(insertSql.toString());
        insertSql.delete(0, insertSql.length());
        if (count > 0) {
            //插入索引文件
            int id = load_index_id("aws_posts_index", "id");
            insertSql.append("INSERT INTO `aws_posts_index` SET `id`='" + id + "' ")
                    .append(" ,`post_id`='" + question_id + "',post_type='question',`add_time`='" + System.currentTimeMillis() / 1000L + "',update_time='" + System.currentTimeMillis() / 1000L + "'")
                    .append(" ,category_id='" + douBanContent.getCategory_id() + "',is_recommend='0',view_count='1',anonymous='0',popular_value='0',uid='1'")
                    .append(" ,`lock`='0',agree_count='0',answer_count='" + douBanContent.getReviews().size() + "'");
            int inde_count = jdbcTemplateObject.update(insertSql.toString());
            if (inde_count > 0 && douBanContent.getReviews().size() > 0) {
                for (String review : douBanContent.getReviews()) {
                    try {
                        insertSql.delete(0, insertSql.length());
                        //插入回复
                        int answer_id = load_index_id("aws_answer", "answer_id");
                        insertSql.append("INSERT INTO `aws_answer` SET `answer_id`='" + answer_id + "' ")
                                .append(" ,`question_id`='" + question_id + "',answer_content='" + review + "',`add_time`='" + System.currentTimeMillis() / 1000L + "',against_count='0'")
                                .append(" ,agree_count='0',`uid`='1',comment_count='0',uninterested_count='0',thanks_count='0',category_id='" + douBanContent.getCategory_id() + "'")
                                .append(" ,`has_attach`='0',ip='2130706433',force_fold='0',anonymous='0'");
                        jdbcTemplateObject.update(insertSql.toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return true;
            }
        }
        return false;
    }

    public int load_index_id(String table_name, String id) {
        String sql = "SELECT MAX(" + id + ") FROM  `" + table_name + "`";
        int i = jdbcTemplateObject.queryForInt(sql);
        return ++i;
    }

}
